#ifndef WORKER_H_
#define WORKER_H_

#include "ace/Task.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_time.h"
#include "ace/Containers.h"
#include "ace/Synch.h"
#include "ace/SString.h"
#include "ace/Method_Request.h"
#include "ace/Future.h"
#include "ace/Activation_Queue.h"
#include "ace/Condition_T.h"

template<class SubWorkerClass> class IManager;

template<class SubWorkerClass>
class IWorker : public ACE_Task<ACE_MT_SYNCH>
{
public:
	IWorker (IManager<SubWorkerClass> *manager) : manager_(manager) { }

	const ACE_Thread_ID& thread_id(void)
	{
		return this->thread_id_;
	}

protected:

	virtual void process_message(ACE_Message_Block *mb) = 0;

private:
	virtual int svc(void)
	{
		ACE_Thread_ID id;
		thread_id_ = id;
		while (1)
		{
			ACE_Message_Block *mb = 0;
			if (this->getq(mb) == -1)
				ACE_ERROR_BREAK((LM_ERROR, ACE_TEXT("%p\n"), ACE_TEXT ("getq")));
			if (mb->msg_type() == ACE_Message_Block::MB_HANGUP)
			{
				ACE_DEBUG((LM_INFO, ACE_TEXT("(%t) Shutting down\n")));
				mb->release();
				break;
			}
			// Process the message.
			process_message(mb);
			// Return to work.
			this->manager_->return_to_work(this);
		}

		return 0;
	}

private:

	IManager<SubWorkerClass> *manager_;
	ACE_Thread_ID thread_id_;
};



#endif //WORKER_H_